import os
import sys

def write_file(filepath, content):
    """写入文件内容"""
    try:
        # 如果文件路径包含目录，则创建目录
        directory = os.path.dirname(filepath)
        if directory:  # 如果有目录部分
            os.makedirs(directory, exist_ok=True)
            
        # 写入文件
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(content)
        print(f"✓ 成功创建文件: {filepath}")
    except Exception as e:
        print(f"✗ 创建文件失败 {filepath}: {str(e)}")

def create_directory_structure():
    """创建基本目录结构"""
    directories = [
        'src',
        'src/capture',
        'src/ui',
        'src/utils',
        'tests',
        'resources'
    ]
    
    for directory in directories:
        try:
            os.makedirs(directory, exist_ok=True)
            print(f"✓ 成功创建目录: {directory}")
        except Exception as e:
            print(f"✗ 创建目录失败 {directory}: {str(e)}")

def setup_project():
    """设置项目结构和文件"""
    print("=== CaptureFlow 项目初始化开始 ===")
    
    # 首先创建目录结构
    create_directory_structure()
    
    # 然后创建文件
    files_to_create = {
        'src/main.py': '''from PyQt5.QtWidgets import QApplication
import sys

def main():
    app = QApplication(sys.argv)
    # TODO: 初始化主窗口
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
''',
        'src/config.py': '''class Config:
    # 快捷键设置
    SHORTCUTS = {
        'screenshot': 'Ctrl+Alt+Q',
        'fullscreen_record': 'Ctrl+Alt+F',
        'area_record': 'Ctrl+Alt+A',
        'start_record': 'Ctrl+Alt+S',
        'stop_record': 'Ctrl+Alt+X'
    }
    
    # 保存路径设置
    DEFAULT_SAVE_PATH = "captures"
    
    # 录制设置
    VIDEO_FPS = 30
    VIDEO_CODEC = 'mp4v'
    AUDIO_CHANNELS = 2
    AUDIO_RATE = 44100
''',
        'src/capture/screenshot.py': '''class ScreenshotTool:
    def __init__(self):
        pass
    
    def capture_area(self):
        # TODO: 实现区域截图
        pass
        
    def save_screenshot(self, image, path):
        # TODO: 保存截图
        pass
''',
        'requirements.txt': '''PyQt5>=5.15.0
opencv-python>=4.5.0
pyaudio>=0.2.11
numpy>=1.19.0
moviepy>=1.0.3
Pillow>=8.0.0
''',
        'tests/test_screenshot.py': '''import unittest

class TestScreenshot(unittest.TestCase):
    def setUp(self):
        pass

    def test_capture_area(self):
        # TODO: 实现测试用例
        pass

if __name__ == '__main__':
    unittest.main()
''',
        '.gitignore': '''# Python
__pycache__/
*.py[cod]
*$py.class
.env
venv/
.idea/
.vscode/
'''
    }

    # 创建所有文件
    for filepath, content in files_to_create.items():
        write_file(filepath, content)

    print("=== 初始化完成 ===")

if __name__ == "__main__":
    # 获取当前工作目录
    current_directory = os.getcwd()
    setup_project()